
package 
{
	import flare.display.TextSprite;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	import flash.utils.*;
	
	
	
	public class Step extends Sprite {
		private var activity:Array;
		
		private var x0:int;
		private var y0:int;
		private var x1:int;
		private var y1:int;
		
		private var color:uint;
		
		private var selected:Boolean;
		
		private var tooltip:TextSprite;
		
		public function Step(profileItem:String, stepColor:uint) {
			activity = profileItem.split(",");
			
			this.selected = false;
			
			tooltip = new TextSprite();
			this.addChild(tooltip);
			
			color = stepColor;
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		public function setXY(coord0:Array, coord1:Array):void {
			x0 = coord0[0];
			y0 = coord0[1];
			x1 = coord1[0];
			y1 = coord1[1];
			this.drawNormal();
		}
		
		private function drawNormal():void {
			graphics.clear();
			
			graphics.lineStyle(7, color);
			graphics.moveTo(x0, y0);
			graphics.lineTo(x1, y1);
		}
		
		private function drawHighlihgt():void {
			graphics.clear();
			
			graphics.lineStyle(10, color + 0x101010);
			graphics.moveTo(x0, y0);
			graphics.lineTo(x1, y1);
		}
		
		private function onMouseOver(e:MouseEvent):void {
			// highlight
			this.drawHighlihgt();
			
			// show tooltip
			tooltip.text = "activities:\n";
			for (var i:int = 0; i < activity.length; i++) {
				tooltip.text += activity[i] + "\n";
				tooltip.x = e.localX + 20;
				tooltip.y = e.localY + 20 * i + 20;
			}
			tooltip.color = 0xffffff;
			tooltip.visible = true;
		}
		
		private function onRollOut(e:MouseEvent):void {
			// resume to normal
			this.drawNormal();
			
			// hide tooltip
			tooltip.visible = false;
		}
		
		private function onClick(e:MouseEvent):void {
			this.selected = !this.selected;  // toggle state
			this.dispatchEvent(new Event("STEP_SELECTED", true));
		}
		
		public function isSelected():Boolean {
			return this.selected;
		}
		
	}
	
}